#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : competitor_price_spider.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-05-22 17:32 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

import datetime
import pandas as pd
from report_system.spider.spider import Spider
from report_system.utils import mail_util
from report_system.utils import num_util
from report_system.utils import date_util
from report_system.utils.log_util import log

from report_system.utils.development_util import debug

if debug:
    receivers = "bianzhiwei@iyoujia.com"
    # receivers = "zhangjiaheng@iyoujia.com"

else:
    receivers = 'dt@iyoujia.com;songweiyun@iyoujia.com;zhanglin@iyoujia.com;zhangjiaheng@iyoujia.com'
    # receivers = "zhangjiaheng@iyoujia.com"

log.info('receivers = ' + receivers)


class Competitor(Spider):
    def __init__(self):
        Spider.__init__(self, spider_num=30 if not debug else 5)
        self.two_bedroom_urls = ['515836', '11844393', '8388787', '9671803', '244866', '10883867', '5715894',
                                 '12282593', '9632225', '8982093', '5818234', '7761454', '5043803']

        self.three_bedroom_urls = ['8502915', '6296705', '298832', '8503720', '11923430', '8823655', '1386581',
                                   '8502936', '362752', '8503027', '7670408']
        self.two_bedroom_list = list()
        self.three_bedroom_list = list()
        self.start_day = date_util.curdate(0)
        self.end_day = date_util.curdate(-1)
        self.check_in_index = self.start_day.day - 1
        self.hour = datetime.datetime.now().hour

    def run(self):

        for two_bedroom_unit_id in self.two_bedroom_urls:
            self.app_detail(unit_id=two_bedroom_unit_id, bedroom=2)

        for three_bedroom_unit_id in self.three_bedroom_urls:
            self.app_detail(unit_id=three_bedroom_unit_id, bedroom=3)

        # self.spider_down()
        # self.req(handler=self.spider_down())

        Spider.run(self)

    def app_detail(self,unit_id, bedroom):
        log.info('unit_id = {unit_id}  ,bedroom = {bedroom}'.format(unit_id=unit_id, bedroom=bedroom))
        unit_id = int(unit_id)
        today = datetime.datetime.now()
        tomorrow = today + datetime.timedelta(days=1)
        url = "https://client.tujia.com/appproduct/getproducts"
        data = {
            "parameter": {
                "activityInfo": "434",
                "bookingCount": 0,
                "checkInDate": today.strftime("%Y-%m-%d"),
                "checkOutDate":tomorrow.strftime("%Y-%m-%d") ,
                "fromOrder": False,
                "fromPromotion": False,
                "peopleCount": "1",
                "productId": 0,
                "unitId": unit_id
            },
            "client": {
                "appId": "com.tujia.hotel",
                "appVersion": "202_202",
                "channelCode": "huawei",
                "devModel": "VKY-AL00",
                "devToken": "13065ffa4e5d97f7834",
                "devType": 2,
                "locale": "zh-CN",
                "osVersion": "9",
                "screenInfo": "54:b1:21:45:05:90,867140031348465",
                "uID": "ad987458-d5d2-398c-996a-be7426e0763e"
            },
            "code": None,
            "psid": "f8467718-a3b0-457a-a8d0-2cb8ede1899d",
            "type": "getProducts",
            "user": None,
            "usid": None
        }
        headers = {
            "User-Agent":"Mozilla/5.0 (Linux; Android 9; VKY-AL00 Build/HUAWEIVKY-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.99 Mobile Safari/537.36 tujia(hotel/202/202 mNet/wifi loc/zh_CN)",
            "Content-Type":"application/json; charset=UTF-8"
        }

        self.req(url=url, method='POST', json=data, headers=headers, bedroom=bedroom,
                 handler=self.app_detail_h, unit_id=unit_id)

    def app_detail_h(self, r, j, unit_id, bedroom):
        log.info(r.headers)
        log.info('unit_id = {unit_id}  ,bedroom = {bedroom}'.format(unit_id=unit_id, bedroom=bedroom))
        # print(unit_id)
        log.info(r.text)
        if r and j and 'content' in j and 'products' in j['content'] and isinstance(j['content']['products'], list):
            price = j['content']['products'][0]['finalPrice']
            # for product in j['data']['products']:
            #     if product['productType'] == 3:
            #         price = product['finalPrice']
            #         if 'totalPriceTip' in product:
            #             price = product['totalPriceTip'] if product['totalPriceTip'] else price
            #         break
            # price = price.replace('1晚¥', '')
            url = 'https://www.tujia.com/detail/{unit_id}.htm'.format(unit_id=unit_id)
            if bedroom == 2:
                self.two_bedroom_list.append([bedroom, unit_id, float(price), url])
            elif bedroom == 3:
                self.three_bedroom_list.append([bedroom, unit_id, float(price), url])
            else:
                raise ValueError("给我停。。。。。。。")
            # self.cal(unit_id, product_id, bedroom)
        else:
            self.req(handler=self.app_detail, unit_id=unit_id, bedroom=bedroom)


    def detail(self, unit_id, bedroom):
        log.info('unit_id = {unit_id}  ,bedroom = {bedroom}'.format(unit_id=unit_id, bedroom=bedroom))
        unit_id = int(unit_id)
        url = 'https://www.tujia.com/bingo/pc/product/v3/getproducts'
        # data = {'checkInDate': str(self.start_day), 'checkOutDate': str(self.end_day), 'unitId': unit_id,
        #         'activityInfo': None, 'callCenter': True}
        data = {
            "unitId": str(unit_id),
            "checkInDate": str(self.start_day),
            "checkOutDate": str(self.end_day),
            "activityInfo": None,
            "fromPromotion": False,
            "callCenter": False
        }
        referer = 'https://www.tujia.com/detail/%s.htm' % unit_id
        log.info('referer = ' + referer)
        headers = {'Content-Type': 'application/json;charset=UTF-8', 'Referer': referer,
                   # User-Agent	Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36
                   '':''}
        self.req(url=url, method='POST', json=data, headers=headers, bedroom=bedroom,
                 handler=self.detail_h, unit_id=unit_id)

    def detail_h(self, r, j, unit_id, bedroom):
        log.info(r.headers)
        log.info('unit_id = {unit_id}  ,bedroom = {bedroom}'.format(unit_id=unit_id, bedroom=bedroom))
        # print(unit_id)
        log.info(r.text)
        if r and j and 'data' in j and 'products' in j['data'] and isinstance(j['data']['products'], list):
            price = j['data']['products'][0]['totalPriceTip']
            for product in j['data']['products']:
                if product['productType'] == 3:
                    price = product['finalPrice']
                    if 'totalPriceTip' in product:
                        price = product['totalPriceTip'] if product['totalPriceTip'] else price
                    break
            price = price.replace('1晚¥', '')
            url = 'https://www.tujia.com/detail/{unit_id}.htm'.format(unit_id=unit_id)
            if bedroom == 2:
                self.two_bedroom_list.append([bedroom, unit_id, float(price), url])
            elif bedroom == 3:
                self.three_bedroom_list.append([bedroom, unit_id, float(price), url])
            else:
                raise ValueError("给我停。。。。。。。")
            # self.cal(unit_id, product_id, bedroom)
        else:
            self.req(handler=self.detail, unit_id=unit_id, bedroom=bedroom)

    def cal(self, unit_id, product_id, bedroom):
        log.info('unit_id = {unit_id}  ,bedroom = {bedroom}'.format(unit_id=unit_id, bedroom=bedroom))
        url = 'https://www.tujia.com/bingo/pc/product/getProductCalendar'
        data = {'productId': product_id, 'unitId': unit_id}
        referer = 'https://www.tujia.com/detail/%d.htm?ssr=off' % unit_id
        headers = {'Content-Type': 'application/json;charset=UTF-8', 'Referer': referer}
        self.req(url=url, method='POST', json=data, headers=headers, handler=self.cal_h, bedroom=bedroom,
                 unit_id=unit_id, product_id=product_id)

    def cal_h(self, r, j, bedroom, unit_id):
        log.info('unit_id = {unit_id}  ,bedroom = {bedroom}'.format(unit_id=unit_id, bedroom=bedroom))
        if j and r and 'data' in j and 'checkIn' in j['data'] and isinstance(j['data']['checkIn'], list):
            log.info('bedroom = ' + str(bedroom))
            log.info('unit_id = ' + str(unit_id))
            log.info('check_in_index = ' + str(self.check_in_index))
            log.info(r.text)
            price = j['data']['checkIn'][self.check_in_index]['price']
            log.info('price = ' + str(price))
            url = 'https://www.tujia.com/detail/{unit_id}.htm'.format(unit_id=unit_id)
            if bedroom == 2:
                self.two_bedroom_list.append([bedroom, unit_id, float(price), url])
            elif bedroom == 3:
                self.three_bedroom_list.append([bedroom, unit_id, float(price), url])
            else:
                raise ValueError("给我停。。。。。。。")

        else:
            self.req(handler=self.detail, unit_id=unit_id, bedroom=bedroom)

    def __del__(self):
        lodge_list = self.two_bedroom_list + self.three_bedroom_list
        # bedroom, unit_id, price, url
        df_detail = pd.DataFrame(lodge_list, columns=['居室', '房源id', '价格', 'url'])
        two_prices = [price[2] for price in self.two_bedroom_list]
        three_prices = [price[2] for price in self.three_bedroom_list]

        two_bedroom_avg = num_util.avg_list(two_prices)
        three_bedroom_avg = num_util.avg_list(three_prices)

        two_bedroom_median = num_util.median_list(two_prices)
        three_bedroom_median = num_util.median_list(three_prices)

        two_dict = {'服务中心': '北京路服务中心', '居室': '两居', '中位数': two_bedroom_median, '均价': two_bedroom_avg}
        three_dict = {'服务中心': '北京路服务中心', '居室': '三居', '中位数': three_bedroom_median, '均价': three_bedroom_avg}

        df_price = pd.DataFrame([two_dict, three_dict])

        df_dict = {'数据详情': df_price, '房源详情': df_detail}
        file_name = title = "实时价格{}点".format(self.hour)
        mail_util.distribute_mail(df_dict=df_dict, file_name=file_name, engine="xlsxwriter",
                                  title=title, receiver=receivers, index=False)

        print('*****' * 20)


def test():
    import requests
    header = {"Host": "www.tujia.com",
              "Upgrade-Insecure-Requests": "1",
              "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
              "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
              "Accept-Encoding": "gzip, deflate, br",
              "Accept-Language": "zh-CN,zh;q=0.9",
              "Cache-Control": "max-age=0",
              "Connection": "keep-alive"

              }

    resp = requests.get('https://www.tujia.com/detail/8388787.htm', headers=header)
    print(resp.headers)


if __name__ == '__main__':
    try:
        Competitor().run()
    except Exception as e:
        mail_util.error_mail("竞对下午房源小时爬虫", e.__str__())
        log.exception(e)
    # test()
